<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php
$_code = $this->getMethodCode();
$_tokens = $this->getTokenList();
?>
<dl class="sp-methods" id="payment_form_<?php echo $_code ?>"
    style="display:none; padding-left: 16px; padding-bottom: 8px; border: 1px solid rgb(193, 193, 193);">
    <dt>
        <input id="p_method_ewayrapid_saved_mastercard" rel="payment_form_mastercard_saved"
               value="<?php echo Eway_Rapid31_Model_Config::CREDITCARD_METHOD ?>" type="radio"
               name="payment[transparent_saved]" title="Check / Money order" class="radio" autocomplete="off" checked="checked">
        <label for="p_method_ewayrapid_saved_mastercard">Master Card </label>

    <ul class="form-list" id="payment_form_mastercard_saved" style="margin-left: 20px;display: block;">
        <li class="saved_token_mastercard_field">
            <select id="ewayrapid_saved_token" name="payment[saved_token]" class="ewayrapid_saved_tokens">
                <?php

                $_defcen = isset($_tokens['default_token']) ? $_tokens['default_token'] : false;
                ?>
                <?php foreach ($_tokens['tokens'] as $_key => $_token):
                    if ($_key == Eway_Rapid31_Model_Config::TOKEN_NEW ||  $this->checkCardName($_token) == Eway_Rapid31_Model_Config::CREDITCARD_METHOD):
                        ?>
                        <?php /* @var Eway_Rapid31_Model_Customer_Token $_token */ ?>
                        <option value="<?php echo $_key ?>"
                                <?php if ($_key == $_defaultToken): ?>selected="selected"<?php endif; ?>>
                            <?php echo $_token->getCard() ?>
                        </option>
                    <?php endif; ?>
                <?php endforeach; ?>
            </select>
            <?php if(Mage::getSingleton('ewayrapid/config')->canEditToken() && count($_tokens['tokens']) > 1): ?>
                <div class="v-fix">
                    <a id="ewayrapid_saved_edit" class="ewayrapid_saved_edit" href="javascript:void(0);"><?php echo $this->__('Edit') ?></a>
                </div>
            <?php endif; ?>
        </li>

        <li class="saved_token_fields" style="display: none">
            <div class="input-box">
                <label for="<?php echo $_code ?>_cc_owner" class="required"><em>*</em><?php echo $this->__('Name on Card') ?></label>
                <input type="text" title="<?php echo $this->__('Name on Card') ?>" class="input-text required-entry" id="<?php echo $_code ?>_cc_owner" name="payment[cc_owner]" value="<?php echo $this->htmlEscape($this->getInfoData('cc_owner')) ?>" />
            </div>
        </li>
        <li class="saved_token_fields" style="display: none">
            <label for="<?php echo $_code ?>_cc_number" class="required"><em>*</em><?php echo $this->__('Credit Card Number') ?></label>
            <div class="input-box help-disabled-cc">
                <input type="text" id="<?php echo $_code ?>_cc_number" data-eway-encrypt-name="payment[cc_number]" title="<?php echo $this->__('Credit Card Number') ?>" class="input-text validate-cc-number validate-cc-type-auto" value="" pattern="\d*"/>
                <input type="hidden" id="<?php echo $_code ?>_cc_type" name="payment[cc_type]" />
                <a href="#"><?php echo $this->__('Help') ?></a>
                <div class="tool-tip" id="cc-help-tool-tip" style="display:none;">
                    <div class="btn-close"><a href="#" id="cc-help-tool-tip-close" title="<?php echo $this->__('Close') ?>"><?php echo $this->__('Close') ?></a></div>
                    <div class="tool-tip-content">To use a new credit card number, please add another card</div>
                </div>
            </div>
        </li>
        <li id="<?php echo $_code ?>_cc_type_exp_div" class="saved_token_fields" style="display: none">
            <label for="<?php echo $_code ?>_expiration" class="required"><em>*</em><?php echo $this->__('Expiration Date') ?></label>
            <div class="input-box">
                <div class="v-fix">
                    <select id="<?php echo $_code ?>_expiration" name="payment[cc_exp_month]" class="month validate-cc-exp required-entry">
                        <?php $_ccExpMonth = $this->getInfoData('cc_exp_month') ?>
                        <?php foreach ($this->getCcMonths() as $k=>$v): ?>
                            <option value="<?php echo $k?$k:'' ?>"<?php if($k==$_ccExpMonth): ?> selected="selected"<?php endif ?>><?php echo $v ?></option>
                        <?php endforeach ?>
                    </select>
                </div>
                <div class="v-fix">
                    <?php $_ccExpYear = $this->getInfoData('cc_exp_year') ?>
                    <select id="<?php echo $_code ?>_expiration_yr" name="payment[cc_exp_year]" class="year required-entry">
                        <?php foreach ($this->getCcYears() as $k=>$v): ?>
                            <option value="<?php echo $k?$k:'' ?>"<?php if($k==$_ccExpYear): ?> selected="selected"<?php endif ?>><?php echo $v ?></option>
                        <?php endforeach ?>
                    </select>
                </div>
            </div>
        </li>
        <?php echo $this->getChildHtml() ?>
        <?php if($this->hasVerification()): ?>
            <li id="<?php echo $_code ?>_cc_type_cvv_div">
                <label for="<?php echo $_code ?>_cc_cid" class="required"><em>*</em><?php echo $this->__('Card Verification Number') ?></label>
                <div class="input-box">
                    <div class="v-fix">
                        <input type="text" title="<?php echo $this->__('Card Verification Number') ?>" class="input-text cvv required-entry validate-cc-cvn" id="<?php echo $_code ?>_cc_cid" data-eway-encrypt-name="payment[cc_cid]" value="" pattern="\d*"/>
                    </div>
                    <a href="#" class="cvv-what-is-this"><?php echo $this->__('What is this?') ?></a>
                </div>
            </li>
            <?php if ($this->getIsRecurring()) { ?>
                <li>
                    <p>I authorize <?php echo Mage::app()->getStore()->getName(); ?> to charge the available balance
                        in my credit card for the future payments of this order. To make a change, I can go to
                        <a href="<?php echo Mage::getUrl('checkout/cart'); ?>">Cart</a>
                    </p>
                    <input type="checkbox" name="recurring-agree" class="recurring-agree required-entry"/><label class="lable-for-recurring" for="">Agree</label>
                </li>
            <?php } ?>
        <?php endif; ?>
    </ul>
    </dt>
    <?php if ($this->getEnablePaypalStandard()) { ?>
        <dt>
            <input id="p_method_ewayrapid_saved_transparent_paypal" rel="payment_form_paypal_saved"
                   value="<?php echo Eway_Rapid31_Model_Config::PAYPAL_STANDARD_METHOD ?>" type="radio"
                   name="payment[transparent_saved]" title="Paypal - eWAY (saved)" class="radio" autocomplete="off">
            <label for="p_method_ewayrapid_saved_transparent_paypal">Paypal Standard </label>

        <ul class="form-list" id="payment_form_paypal_saved" style="margin-left: 20px;display: none;">
            <li class="saved_token_paypal_field">
                <select id="ewayrapid_saved_tokens" name="payment[paypal_saved_token]" class="ewayrapid_saved_tokens">
                    <?php

                    $_defaultToken = isset($_tokens['default_token']) ? $_tokens['default_token'] : false;
                    $existed = false;
                    ?>
                    <?php foreach ($_tokens['tokens'] as $_key => $_token):
                        if ($this->checkCardName($_token) == Eway_Rapid31_Model_Config::PAYPAL_STANDARD_METHOD):
                            $existed = true; ?>
                            <?php /* @var Eway_Rapid31_Model_Customer_Token $_token */ ?>
                            <option value="<?php echo $_key ?>"
                                    <?php if ($_key == $_defaultToken): ?>selected="selected"<?php endif; ?>>
                                <?php echo $_token->getCard() ?>
                            </option>
                        <?php endif; ?>
                    <?php endforeach; ?>

                    <?php if(!$existed): ?>
                        <option value="<?php echo Eway_Rapid31_Model_Config::TOKEN_NEW ?>">
                            <?php echo $this->__('Add new card') ?>
                        </option>
                    <?php endif; ?>
                </select>
            </li>
        </ul>
        </dt>
    <?php } ?>

    <?php if ($this->getEnableMasterPass()) { ?>
        <dt>
            <input id="p_method_ewayrapid_transparent_masterpass" rel="payment_form_masterpass_saved"
                   value="<?php echo Eway_Rapid31_Model_Config::MASTERPASS_METHOD ?>" type="radio"
                   name="payment[transparent_saved]" title="MasterPass - eWAY (saved)" class="radio" autocomplete="off">
            <label for="p_method_ewayrapid_transparent_masterpass">MasterPass </label>

        <ul class="form-list" id="payment_form_masterpass_saved" style="margin-left: 20px;display: none;">
            <li class="saved_token_masterpass_field">
                <select id="ewayrapid_saved_tokens" name="payment[masterpass_saved_token]" class="ewayrapid_saved_tokens">
                    <?php

                    $_defaultToken = isset($_tokens['default_token']) ? $_tokens['default_token'] : false;
                    $existed = false;
                    ?>
                    <?php foreach ($_tokens['tokens'] as $_key => $_token):
                        if ($_key == Eway_Rapid31_Model_Config::TOKEN_NEW || $this->checkCardName($_token) == Eway_Rapid31_Model_Config::MASTERPASS_METHOD):
                            $existed = true; ?>
                            <?php /* @var Eway_Rapid31_Model_Customer_Token $_token */ ?>
                            <option value="<?php echo $_key ?>"
                                    <?php if ($_key == $_defaultToken): ?>selected="selected"<?php endif; ?>>
                                <?php echo $_token->getCard(); ?>
                            </option>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </select>
            </li>
        </ul>
        </dt>
    <?php } ?>
</dl>

<script type="text/javascript">
    var ewayPaymentToken = new EwayPaymentToken(
        <?php echo $_tokens['tokens_json'] ?>,
        <?php echo count($_tokens['tokens']) ?>,
        false,
        '<?php echo $this->__('Edit') ?>',
        '<?php echo $this->__('Cancel edit') ?>'
    )

    $$('.help-disabled-cc a').each(function (element) {
        Event.observe(element, 'click', toggleToolTipCc);
    });

    function toggleToolTipCc(event) {
        if ($('cc-help-tool-tip')) {
            $('cc-help-tool-tip').toggle();
        }
        Event.stop(event);
    }
    if ($('cc-help-tool-tip-close')) {
        Event.observe($('cc-help-tool-tip-close'), 'click', toggleToolTipCc);
    }

    //    $('p_method_ewayrapid_saved_mastercard').observe('click',function(el){
    //        $('payment_form_mastercard').show();
    //    });

    $$('.radio').each(function (el) {
        var _rel = $(el).readAttribute('rel');
        //console.log(_rel);
        if ($(_rel)) {
            $(el).observe('click', function () {
                $$('.form-list').each(function (rd) {
                    rd.hide();
                });
                $(_rel).show();
                $(_rel).firstElementChild.show();
            });
        }
    });

    /*$('payment_form_mastercard_saved').show();
    $$('.saved_token_mastercard_field').each(function(cl) {
        cl.show();
    });*/

</script>